// Copyright 2009, Google Inc. All Rights Reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * This code sample retrieves the list of datamaps
 * for specified profile.
 */

using System;
using System.Xml;
using System.Xml.XPath;

namespace ReportServiceGetTableListREST
{
	public class GetTableList
	{
		// Provide login information.
		private const string login = "YOUR_LOGIN";
		private const string password = "YOUR_PASSWORD";
		private const int profileId = 16;

		private const string serviceURL = "http://SERVER_NAME:URCHIN_PORT/services/v1/reportservice/tables/";

		static void Main()
		{
			try
			{
				// Compose REST URL.
				string url = serviceURL + "?login=" + login + "&password=" + password + "&profileId=" + profileId;
				// Retrieve table list.
				XPathDocument xPathDocument = new XPathDocument(url);
				// Process response XML.
				XPathNavigator xPathNavigator = xPathDocument.CreateNavigator();
				XmlNameTable xmlNameTable = new NameTable();
				XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlNameTable);
				xmlNamespaceManager.AddNamespace("tns", "https://urchin.com/api/urchin/v1/");
				// Get all tables.
				XPathNodeIterator tableXPathNodeIterator = xPathNavigator.Select("/tns:getTableListResponse/table", xmlNamespaceManager);
				// Display tables info.
				while (tableXPathNodeIterator.MoveNext())
					DisplayTable(tableXPathNodeIterator.Current);
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}
		}

		private static void DisplayTable(XPathNavigator tableXPathNavigator)
		{
			Console.WriteLine("Table id is \"" + tableXPathNavigator.SelectSingleNode("tableId/text()") + "\", ");
			// Display the table dimensions.
			Console.WriteLine("dimensions are:");
			XPathNodeIterator dimensionsXPathNodeIterator = tableXPathNavigator.Select("dimensions/dimension/text()");
			while (dimensionsXPathNodeIterator.MoveNext())
				Console.WriteLine("\t\"" + dimensionsXPathNodeIterator.Current.Value + "\", ");
			// Display the table metrics.
			Console.WriteLine("metrics are:");
			XPathNodeIterator metricsXPathNodeIterator = tableXPathNavigator.Select("metrics/metric/text()");
			while (metricsXPathNodeIterator.MoveNext())
				Console.WriteLine("\t\"" + metricsXPathNodeIterator.Current.Value + "\", ");
		}
	}
}
